package com.csdj.server.controller;

import com.csdj.server.pojo.Admin;
import com.csdj.server.pojo.ChatMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * @Author zyq
 * @Date 2023/2/13 9:52
 * @ClassName: WsController
 * @Description: TODO
 * @Version 1.0
 */
@Controller
public class WsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMsg chatMsg){
        //根据 security的 Authentication
        Admin admin = (Admin) authentication.getPrincipal();
        chatMsg.setForm(admin.getUsername());
        chatMsg.setFromNickName(admin.getName());
        chatMsg.setDate(LocalDateTime.now());
        //消息 发送处理
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),
                "/queue/chat",chatMsg);
    }

}
